// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// <reference path='/socket.io/socket.io.js'/>
// /<reference path='socket.io.js'/>
// /<reference path='../../node_modules/socket.io/lib/socket.io.js'/>

var serverURL;
var mirrorClient;
if (window.location.protocol === 'https:') {
  serverURL = 'wss://localhost:8000/projector';
} else {
  serverURL = 'ws://localhost:3000/projector';
}
console.log(window.location.protocol, 'serverURL')
  // var serverURL = 'ws://localhost:3000/projector';
  // console.log(window.location,'window.location')
  // var serverURL = 'ws://localhost:3000/projector';
  // var serverURL = 'ws://' + location.host + '/projector';


var socket;

function socketSend(msg) {
  socket.emit('projector', JSON.stringify(msg));
}

// window.addEventListener('load', function() {
//   alert('load')
//   chrome.extension.sendMessage({ mirror : true}, function(response) {
//     alert(response.mirror+'response')
//     if (response.mirror)
//       startMirroring();
//     else
//       stopMirroring();
//   });
// });
window.addEventListener('load', function() {
  // alert('load')
  chrome.extension.sendMessage({
    mirror: true
  }, function(response) {
    // alert(response.mirror+':response.mirror')
    if (response.mirror)
      startMirroring();
    else
      stopMirroring();
  });
});

function startMirroring() {
  // alert('startMirroring');
  // 启动前先关闭原来的mirror
  stopMirroring();
  if (socket) {
    alert(socket + 'if (true) {}');
    return;
  }
  if (window.location.protocol === 'https:') {
    serverURL = 'wss://localhost:8000/projector';
    socket = io.connect(serverURL,{secure: true});
  } else {
    serverURL = 'ws://localhost:3000/projector';
    socket = io.connect(serverURL);
  }
  // socket = new WebSocket(serverURL);
  socket = io.connect(serverURL);
  // alert(socket);
  console.log(socket, 'socket')
  socket.on('connect', function() {
    console.log('client connection');
    socketSend({
      base: location.href.match(/^(.*\/)[^\/]*$/)[1]
    });
    socketSend({
      crawler_url: location.href
    });
    mirrorClient = new TreeMirrorClient(document, {
      initialize: function(rootId, children) {
        socketSend({
          f: 'initialize',
          args: [rootId, children]
        });
      },

      applyChanged: function(removed, addedOrMoved, attributes, text) {
        socketSend({
          f: 'applyChanged',
          args: [removed, addedOrMoved, attributes, text]
        });
      }
    });
  });



  socket.on('disconnect', function() {
    console.log('disconnect')
    mirrorClient.disconnect();
    socket = undefined;
  });
}

function stopMirroring() {
  // alert('stopMirroring')
  console.log('stopMirroring')
  if (socket)
    socket.disconnect();
  socket = undefined;
}
